package cc.rengu.igas.smps.biz.service.impl;

import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.common.enums.RespCodeEnum;
import cc.rengu.igas.smps.facade.RiskAlarmService;
import cc.rengu.igas.smps.facade.request.AlarmNoticeRequest;
import cc.rengu.igas.smps.facade.request.IdentityNetworkVerifyRequest;
import cc.rengu.igas.smps.facade.request.PersonalCreditInquiryRequest;
import cc.rengu.igas.smps.facade.response.AlarmNoticeResponse;
import cc.rengu.igas.smps.facade.response.IdentityNetworkVerifyResponse;
import cc.rengu.igas.smps.facade.response.PersonalCreditInquiryResponse;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by WKF on 2020-04-09
 *
 * @author WKF
 */
public class RiskAlarmServiceImpl implements RiskAlarmService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<AlarmNoticeResponse> alarmNotice(AlarmNoticeRequest alarmNoticeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AlarmNoticeResponse> result = new Result<>();
        AlarmNoticeResponse alarmNoticeResponse = new AlarmNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(alarmNoticeRequest, SmpsServiceConstant.ALARM_NOTICE_SERVICE, AlarmNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AlarmNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("告警平台告警推送失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                alarmNoticeResponse.setRespCode(((BizException) e).getCode());
                alarmNoticeResponse.setRespDesc(e.getMessage());
            } else {
                alarmNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                alarmNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<PersonalCreditInquiryResponse> personalCreditInquiry(PersonalCreditInquiryRequest personalCreditInquiryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<PersonalCreditInquiryResponse> result = new Result<>();
        PersonalCreditInquiryResponse personalCreditInquiryResponse = new PersonalCreditInquiryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(personalCreditInquiryRequest, SmpsServiceConstant.PERSONAL_CREDIT_INQUIRY_SERVICE, PersonalCreditInquiryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((PersonalCreditInquiryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("个人征信报告查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                personalCreditInquiryResponse.setRespCode(((BizException) e).getCode());
                personalCreditInquiryResponse.setRespDesc(e.getMessage());
            } else {
                personalCreditInquiryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                personalCreditInquiryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<IdentityNetworkVerifyResponse> identityNetworkVerify(IdentityNetworkVerifyRequest identityNetworkVerifyRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<IdentityNetworkVerifyResponse> result = new Result<>();
        IdentityNetworkVerifyResponse identityNetworkVerifyResponse = new IdentityNetworkVerifyResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(identityNetworkVerifyRequest, SmpsServiceConstant.IDENTITY_NETWORK_VERIFY_SERVICE, IdentityNetworkVerifyResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((IdentityNetworkVerifyResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("身份联网核查失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                identityNetworkVerifyResponse.setRespCode(((BizException) e).getCode());
                identityNetworkVerifyResponse.setRespDesc(e.getMessage());
            } else {
                identityNetworkVerifyResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                identityNetworkVerifyResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
